using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.Sdk.Sfc;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.UI;
using System;
using System.Data;
using System.Globalization;
using System.Windows.Forms;

namespace Microsoft.SqlServer.Management.SqlManagerUI
{
	internal class GenWizObjectType
	{
		private string title;

		private string subTitle;

		private string helpTopicKeyword;

		private string description;

		private string selectedText;

		private string progressText;

		private bool isSelected;

		private GenScriptWizForm.ScriptWizardPage pageId;

		private string friendlyName;

		private string enumeratorPath;

		private bool schemaBased;

		private UrnCollection selectedUrns = new UrnCollection();

		private WizardInputs wizardInputs;

		private int itemCount;

		private int minimumVersion = 8;

		private Type smoType;

		public string PageTitle
		{
			get
			{
				return this.title;
			}
			set
			{
				this.title = value;
			}
		}

		public string PageSubTitle
		{
			get
			{
				return this.subTitle;
			}
			set
			{
				this.subTitle = value;
			}
		}

		public string HelpTopicKeyword
		{
			get
			{
				return this.helpTopicKeyword;
			}
			set
			{
				this.helpTopicKeyword = value;
			}
		}

		public string PageDescription
		{
			get
			{
				return this.description;
			}
			set
			{
				this.description = value;
			}
		}

		public GenScriptWizForm.ScriptWizardPage PageId
		{
			get
			{
				return this.pageId;
			}
			set
			{
				this.pageId = value;
			}
		}

		public string FriendlyName
		{
			get
			{
				return this.friendlyName;
			}
			set
			{
				this.friendlyName = value;
			}
		}

		public string ProgressText
		{
			get
			{
				return this.progressText;
			}
			set
			{
				this.progressText = value;
			}
		}

		public bool SchemaBased
		{
			get
			{
				return this.schemaBased;
			}
			set
			{
				this.schemaBased = value;
			}
		}

		public string SelectedText
		{
			get
			{
				return this.selectedText;
			}
			set
			{
				this.selectedText = value;
			}
		}

		public bool IsSelected
		{
			get
			{
				return this.isSelected;
			}
			set
			{
				this.isSelected = value;
			}
		}

		public UrnCollection SelectedUrns
		{
			get
			{
				return this.selectedUrns;
			}
		}

		public WizardInputs WizardInputs
		{
			get
			{
				return this.wizardInputs;
			}
			set
			{
				this.wizardInputs = value;
			}
		}

		public int MinimumSupportedVersion
		{
			get
			{
				return this.minimumVersion;
			}
			set
			{
				this.minimumVersion = value;
			}
		}

		public Type SmoType
		{
			get
			{
				return this.smoType;
			}
			set
			{
				this.smoType = value;
			}
		}

		public int TotalItemCount
		{
			get
			{
				return this.itemCount;
			}
		}

		public string EnumeratorPath
		{
			get
			{
				return this.enumeratorPath;
			}
			set
			{
				if (value[0] == '/')
				{
					this.enumeratorPath = value.Substring(1);
					return;
				}
				this.enumeratorPath = value;
			}
		}

		public bool Exists
		{
			get
			{
				bool result = false;
				GenWizGeneralData genWizGeneralData = (GenWizGeneralData)this.wizardInputs.FindItem(0);
				if (genWizGeneralData.SqlServerVersion >= this.minimumVersion)
				{
					Request request = new Request(this.GetUrn());
					request.Fields = new string[]
					{
						"Name"
					};
					GenWizGeneralData genWizGeneralData2 = (GenWizGeneralData)this.wizardInputs.FindItem(0);
					SqlConnectionInfo connectionInfo = genWizGeneralData2.ConnectionInfo;
					DataTable dataTable = Enumerator.GetData(connectionInfo, request);
					result = (dataTable.Rows.Count > 0);
					dataTable.Dispose();
				}
				return result;
			}
		}

		private string GetUrn()
		{
			GenWizGeneralData genWizGeneralData = (GenWizGeneralData)this.wizardInputs.FindItem(0);
			return string.Format(CultureInfo.InvariantCulture, "Server/Database[@Name='{0}']/{1}", new object[]
			{
				Urn.EscapeString(genWizGeneralData.DatabaseName),
				this.enumeratorPath
			});
		}

		public DataTable GetItems()
		{
			Request request = new Request(this.GetUrn());
			if (this.schemaBased)
			{
				request.Fields = new string[]
				{
					"Urn",
					"Name",
					"Schema"
				};
				request.OrderByList = new OrderBy[]
				{
					new OrderBy("Schema", OrderBy.Direction.Asc),
					new OrderBy("Name", OrderBy.Direction.Asc)
				};
			}
			else
			{
				request.Fields = new string[]
				{
					"Urn",
					"Name"
				};
				request.OrderByList = new OrderBy[]
				{
					new OrderBy("Name", OrderBy.Direction.Asc)
				};
			}
			GenWizGeneralData genWizGeneralData = (GenWizGeneralData)this.wizardInputs.FindItem(0);
			SqlConnectionInfo connectionInfo = genWizGeneralData.ConnectionInfo;
			DataTable dataTable = Enumerator.GetData(connectionInfo, request);
			this.itemCount = dataTable.Rows.Count;
			return dataTable;
		}

		internal void AddObjectDesc(TreeNode topNode)
		{
			TreeNode treeNode = null;
			treeNode = new TreeNode();
			treeNode.Text = this.selectedText;
			if (this.selectedUrns.Count == this.itemCount)
			{
				TreeNode treeNode2 = new TreeNode();
				treeNode2.Text = GenScriptWizSR.GenScriptSelectFormAll;
				treeNode.Nodes.Add(treeNode2);
			}
			else
			{
				foreach (Urn current in this.selectedUrns)
				{
					TreeNode treeNode3 = new TreeNode();
					if (this.schemaBased)
					{
						treeNode3.Text = current.GetAttribute("Schema") + "." + current.GetAttribute("Name");
					}
					else
					{
						treeNode3.Text = current.GetAttribute("Name");
					}
					treeNode.Nodes.Add(treeNode3);
				}
			}
			topNode.Nodes.Add(treeNode);
		}
	}
}
